package com.snail.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snail.mapper.BizCollectionWorksMapper;
import com.snail.mapper.BizWorksMapper;
import com.snail.model.pojo.media.BizCollection;
import com.snail.model.pojo.media.BizCollectionWorks;
import com.snail.model.pojo.media.BizWorks;
import com.snail.model.query.media.CollectionAddQuery;
import com.snail.model.query.media.CollectionListQuery;
import com.snail.model.vo.media.CollectionWorksDetailVo;
import com.snail.service.BizCollectionService;
import com.snail.mapper.BizCollectionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Monica
* @description 针对表【biz_collection(合集主表)】的数据库操作Service实现
* @createDate 2025-08-19 10:55:07
*/
@Service
public class BizCollectionServiceImpl extends ServiceImpl<BizCollectionMapper, BizCollection>
    implements BizCollectionService{

    @Autowired
    BizCollectionMapper bizCollectionMapper;

    @Autowired
    BizCollectionWorksMapper bizCollectionWorksMapper;

    @Autowired
    BizWorksMapper bizWorksMapper;
    @Override
    public Page<BizCollection> getCollectionList(CollectionListQuery query) {
        Page<BizCollection> page = new Page<>(query.getPageNum(), query.getPageSize());
        LambdaQueryWrapper<BizCollection> wrapper = new LambdaQueryWrapper<>();

        if (StrUtil.isNotBlank(query.getName())) {
            wrapper.like(BizCollection::getName, query.getName());
        }
        if (query.getStatus() != null) {
            wrapper.eq(BizCollection::getStatus, query.getStatus());
        }
        wrapper.eq(BizCollection::getUserId, query.getUserId());
        return bizCollectionMapper.selectPage(page, wrapper);
    }

    @Override
    public BizCollection addCollection(CollectionAddQuery query) {
        BizCollection bizCollection = new BizCollection();
        BeanUtils.copyProperties(query, bizCollection);
        bizCollectionMapper.insert(bizCollection);
        List<Long> worksId = query.getWorksId();
        for (Long workId : worksId) {
            BizCollectionWorks bizCollectionWorks = new BizCollectionWorks();
            bizCollectionWorks.setCollectionId(bizCollection.getId());
            bizCollectionWorks.setWorksId(workId);
            bizCollectionWorks.setSort(worksId.indexOf(workId));
            bizCollectionWorksMapper.insert(bizCollectionWorks);
        }
        return bizCollection;
    }

    @Override
    public CollectionWorksDetailVo getCollectionWorksDetail(Integer collectionId) {
        BizCollection bizCollection = bizCollectionMapper.selectById(collectionId);
        LambdaQueryWrapper<BizCollectionWorks> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BizCollectionWorks::getCollectionId, collectionId);
        List<BizCollectionWorks> bizCollectionWorks = bizCollectionWorksMapper.selectList(queryWrapper);
        ArrayList<BizWorks> bizWorks = new ArrayList<>();
        for (BizCollectionWorks item : bizCollectionWorks) {
            BizWorks works = bizWorksMapper.selectById(item.getWorksId());
            bizWorks.add(works);
        }
        CollectionWorksDetailVo collectionWorksDetailVo = new CollectionWorksDetailVo();
        BeanUtils.copyProperties(bizCollection, collectionWorksDetailVo);
        collectionWorksDetailVo.setBizWorksList(bizWorks);
        return collectionWorksDetailVo;
    }

    @Override
    public List<CollectionWorksDetailVo> getCollectionByWorkId(Integer workId) {
        //查询视频对应的作品集合
        LambdaQueryWrapper<BizCollectionWorks> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BizCollectionWorks::getWorksId, workId).select(BizCollectionWorks::getCollectionId);
        // 先获取实体对象（可能为 null，建议判空）
        List<BizCollectionWorks> collectionWorks = bizCollectionWorksMapper.selectList(queryWrapper);
        //获取作品所在的所有collection的id
        List<Long> collectionIdList = new ArrayList<>();
        //先剔除不存在的集合
        for (BizCollectionWorks item : collectionWorks) {
            if (item.getCollectionId() != null) { // 先过滤空的collectionId
                collectionIdList.add(item.getCollectionId());
            }
        }
        System.out.println("list为"+collectionIdList);
       List<CollectionWorksDetailVo> result = new ArrayList<>();
        for (Long collectionId : collectionIdList) {
            //剔除不存在的collection的id
            BizCollection bizCollection = bizCollectionMapper.selectById(collectionId);
            if (bizCollection == null) { // 跳过不存在的合集记录
                continue;
            }
            //保持getCollectionWorksDetail当中的查询逻辑
            //BizCollection bizCollection = bizCollectionMapper.selectById(collectionId);
            LambdaQueryWrapper<BizCollectionWorks> queryWrapperDetail = new LambdaQueryWrapper<>();
            queryWrapperDetail.eq(BizCollectionWorks::getCollectionId, collectionId);
            List<BizCollectionWorks> bizCollectionWorks = bizCollectionWorksMapper.selectList(queryWrapperDetail);
            ArrayList<BizWorks> bizWorks = new ArrayList<>();
            for (BizCollectionWorks item : bizCollectionWorks) {
                BizWorks works = bizWorksMapper.selectById(item.getWorksId());
                bizWorks.add(works);
            }
            CollectionWorksDetailVo collectionWorksDetailVo = new CollectionWorksDetailVo();
            BeanUtils.copyProperties(bizCollection, collectionWorksDetailVo);
            collectionWorksDetailVo.setBizWorksList(bizWorks);
            result.add(collectionWorksDetailVo);
        }


        return result;
    }
}




